Apply [ThreadStatic] attribute to a method in external assembly
Posted
by
Sen Jacob
on Stack Overflow
See other posts from Stack Overflow
or by Sen Jacob
Published on 2013-10-28T15:52:37Z
Indexed on
2013/10/28
21:54 UTC
Read the original article
Hit count: 208
Can I use an external assembly's static method
like [ThreadStatic]
method?
Here is my situation.
The assembly class
(which I do not have access to its source) has this structure
public class RegistrationManager()
{
private RegistrationManager() {}
public static void RegisterConfiguration(int ID) {}
public static object DoWork() {}
public static void UnregisterConfiguration(int ID) {}
}
Once registered, I cannot call the DoWork()
with a different ID without unregistering the previously registered one. Actually I want to call the DoWork()
method with different IDs simultaneously with multi-threading.
If the RegisterConfiguration(int ID)
method was [ThreadStatic]
, I could have call it in different threads without problems with calls, right? So, can I apply the [ThreadStatic]
attribute to this method or is there any other way I can call the two static
methods same time without waiting for other thread to unregister it?
If I check it like the following, it should work.
for(int i=0; i < 10; i++)
{
new Thread(new ThreadStart(() => Checker(i))).Start();
}
public string Checker(int i)
{
public static void RegisterConfiguration(i); // Now i cannot register second time
public static object DoWork(i);
Thread.Sleep(5000); // DoWork() may take a little while to complete before unregistered
public static void UnregisterConfiguration(i);
}
© Stack Overflow or respective owner